import React, { useState, useEffect, useRef } from 'react';
import { createModel } from 'hox';
import { message } from 'antd';
import { dictionaryImport, queryDictionary } from './service.js';
import { useRequest } from '@umijs/hooks';

class Field {}
function useModel() {
  const field = new Field();

  const [state, dispatch] = useState({
    ...field,
  });
  const clear = () => {
    dispatch({ ...field });
  };

  const setState = payload => {
    dispatch(prevState => ({ ...prevState, ...payload }));
  };
  const { loading, run, data } = useRequest(queryDictionary, {
    manual: true,
    onSuccess: (result, params) => {},
  });
  //导入电信配置
  const dictionaryImportProps = useRequest(dictionaryImport, {
    manual: true,
    onSuccess: (res, params) => {
      if (res.result) {
        message.success('导入成功！');
        let row = localStorage.getItem('deviceType');
        run({ pageSize: 9999, currentPage: 1, fsuTypeId: row ? JSON.parse(row)?.id : '' });
      } else {
        message.error(res.msg);
      }
    },
  });
  return {
    run,
    state: {
      ...state,
      list: data?.data?.dataList || [],
      total: data?.data?.total || 0,
      dictionaryImportProps,
    },
    clear,
    setState,
  };
}

export default createModel(useModel);
